package sg.nus.iss.vmcs.gui;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import sg.nus.iss.vmcs.controllers.MainController;
import sg.nus.iss.vmcs.controllers.SimulationController;
import sg.nus.iss.vmcs.guilisteners.ActivateCustomerPanelButtonListener;
import sg.nus.iss.vmcs.guilisteners.ActivateMachineryPanelButtonListener;
import sg.nus.iss.vmcs.guilisteners.ActivateMaintainerPanelButtonListener;
import sg.nus.iss.vmcs.guilisteners.BeginSimulationButtonListener;
import sg.nus.iss.vmcs.guilisteners.EndSimulationButtonListener;

/**
 * Main Simulator screen used to control all simulation functions
 * @author Olivo Motto
 */
public class SimulatorControlPanel extends Frame {

    /** Generated Serial ID */
	private static final long serialVersionUID = -8877654117858955579L;

	//Static Declarations
    public static final char BEGIN = 0;
    public static final char END = 1;
    public static final char ACT_CUSTOMER = 2;
    public static final char ACT_MAINTAINER = 3;
    public static final char ACT_MACHINERY = 4;
    public static final char ACT_EXIT = 5;
   
	private static final String title = "Simulation Control Panel";
    private static final String labels[] = {
		"Begin Simulation",
		"End Simulation",
		"Activate Customer Panel",
		"Activate Maintainer Panel",
		"Activate Machinery Panel"
    };

    //Controllers
    private MainController   		mainCtrl;
    private SimulationController    simulationCtrl;
    
    //UI Controls
    private Button buttons[] = new Button [labels.length];
    private Font titleFont	 = new Font ("Helvetica", Font.BOLD, 18);
    private Font buttonFont	 = new Font ("Helvetica", Font.BOLD, 14);

    /**
     * Creates a reference of the Simulator Control Panel
     * @param cont
     */
    public SimulatorControlPanel (SimulationController cont) {
        super (title);

        simulationCtrl = cont;
        mainCtrl = simulationCtrl.getMainController();

        for (int i = 0; i < labels.length; i++) {
            createButton(i, cont);
        }

        setLayout(new GridLayout(0, 1));

        add (createPanelLabel());
        add (buttons[BEGIN]);
        add (buttons[ACT_CUSTOMER]);
        add (buttons[ACT_MAINTAINER]);
        add (buttons[ACT_MACHINERY]);
        add (buttons[END]);

        pack();
        setLocation(100, 100);

        addWindowListener(new WindowAdapter() {
		    public void windowClosing(WindowEvent e) {
		    	simulationCtrl.getMainController().closeDown();
		    }
        });
    }

    /**
     * Sets the Visibility of the UI to True
     */
    public void display(){
    	this.setVisible(true);
    }

    /** 
     *	Creates the Title Label for this UI
     */
    private Label createPanelLabel() {
        Label l = new Label (title);
        l.setBackground(Color.blue);
        l.setForeground(Color.white);
        l.setFont(titleFont);
        return (l);
    }

    /**
     * Creates the buttons used by the Simulator Control Panel
     * Added by Philip on 29/12/2010
     * @param id Index of the button
     * @param cont Reference to Simulation Controller
     */
    private void createButton(int id, SimulationController cont) {
        Button button = new Button (labels[id]);
        button.setBackground(Color.white);
        button.setForeground(Color.blue);
        button.setFont(buttonFont);
        
        switch (id){
        case SimulatorControlPanel.BEGIN :
        	button.addActionListener(new BeginSimulationButtonListener(simulationCtrl));
            break;
        case SimulatorControlPanel.END :
        	button.addActionListener(new EndSimulationButtonListener(mainCtrl));
            break;
        case SimulatorControlPanel.ACT_MAINTAINER :
        	button.addActionListener(new ActivateMaintainerPanelButtonListener(simulationCtrl));
            break;
        case SimulatorControlPanel.ACT_MACHINERY :
        	button.addActionListener(new ActivateMachineryPanelButtonListener(simulationCtrl));
            break;
        case SimulatorControlPanel.ACT_CUSTOMER :
        	button.addActionListener(new ActivateCustomerPanelButtonListener(simulationCtrl));
        	break;
        }
        
        //Reassign button back into array
        buttons [id] = button;
    }

    /**
     *	Sets the Simulation to Active or Inactive modes 
     */
    public void setSimulationActive (boolean isOn) {
        buttons[BEGIN].setEnabled (!isOn);
        buttons[ACT_MAINTAINER].setEnabled(isOn);
        buttons[ACT_CUSTOMER].setEnabled(isOn);
        buttons[ACT_MACHINERY].setEnabled (isOn);
        buttons[END].setEnabled (isOn);
    }

    /**
     * Sets the specific button state for a button
     * @param id Index of the Button
     * @param state true to activate the button, false to deactivate
     */
    public void setButtonState (char id, boolean state) {
        buttons[id].setEnabled (state);
    }

    /**
     * Closes the SImulation
     */
    public void closeDown () {
        dispose ();
    }

    /**
     * Stops the Simulation
     */
    public void stop(){
    	mainCtrl.closeDown();
    }

    /**
     * Sets a particular button's state
     * @param cmp Index of the button
     * @param st true for active, false if otherwise
     */
    public void setActive(char  cmp, boolean st){
    	setButtonState(cmp, st);
    }
}